package org.itboys.core.service.mongo.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.goods.SundayGoodsDto;
import org.itboys.core.dto.goods.SundayGoodsSizeDto;
import org.itboys.core.entity.mongo.SundayGoods;
import org.itboys.core.entity.mongo.SundayGoodsSize;
import org.itboys.core.util.JNBYRequestUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2018/10/15
 * 描述：场景商品
 */
@Service
public class SundayGoodsService extends BaseMongoService<SundayGoods> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private SundayGoodsSizeService goodsSizeService;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayGoods> getEntityClass() {
        return SundayGoods.class;
    }

    /**
     * @param goods
     */
    public void save(SundayGoods goods, List<String> sizeDetails,List<String> newSizeDetails, List<Double> costPrices, List<Double> discountPrices,
                     List<String> goodsCodes, List<Long> codeIds, List<Long> goodsIds) {
        if (goods.getId() == 0L) {
            super.save(goods);
        } else {
            super.updateExceptEmpty(goods.getId(), goods);
            //删除旧尺码
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("sundayGoodsId",goods.getId());
            goodsSizeService.delete(param);
        }
        //新增尺码
        List<SundayGoodsSize> sizes = new ArrayList<>();
        for (int i = 0; i < sizeDetails.size(); i++) {
            SundayGoodsSize goodsSize = new SundayGoodsSize(goods.getId(), goodsCodes.get(i), codeIds.get(i),
                    goodsIds.get(i), sizeDetails.get(i),newSizeDetails.get(i), costPrices.get(i), discountPrices.get(i));
            sizes.add(goodsSize);
        }
        goodsSizeService.batchSave(sizes);
    }

    /**
     * @param id
     * @return
     */
    public SundayGoodsDto findOne(Long id) {
            SundayGoodsDto dto = null;
            if (LongUtil.isNotZreo(id)) {
                dto = packageSingleDto(super.getById(id));
                Map<String,Object> param=new HashMap<String,Object>();
                param.put("isDeleted",SundayCommonConstants.NO);
                param.put("sundayGoodsId",id);
                List<SundayGoodsSizeDto> sizeDtos=goodsSizeService.packageMultiDto(goodsSizeService.list(param));
                dto.setSizeDtos(sizeDtos);
            } else {
                dto = new SundayGoodsDto();
                dto.setId(id);
            }
            return dto;
        }


    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageResult<SundayGoodsDto> select(HttpServletRequest request) throws IOException {
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request, "sceneId");
        param.put("isDeleted", 0);
        Map<String, String> containsparam = ParamUtil.packageMongoVagueParam(request, "");
        PageResult<SundayGoods> pageResuls = super.containpageQuery(request, containsparam, param);
        return new PageResult<SundayGoodsDto>(packageMultiDto(pageResuls.getData()), pageResuls.getTotal());
    }

    /**
     * 获取全部
     *
     * @param request
     * @return
     * @throws IOException
     */
    public PageResult<SundayGoodsDto> selectAll(HttpServletRequest request) throws IOException {
        String name = request.getParameter("name");
        JSONObject paramJson = new JSONObject();
        Integer offset = Integer.valueOf(request.getParameter("offset"));
        paramJson.put("pageNo", offset + 1);
        paramJson.put("pageSize", request.getParameter("limit"));
        if (StringUtils.isNotBlank(name)) {
            paramJson.put("goodsName", name);
        }
        JSONObject result = JNBYRequestUtil.request(paramJson, "108d1f62b2e7f5a755230479f5eff13d", SundayCommonConstants.goods_select_url);
        List<JSONObject> goodJsonList = (List<JSONObject>) result.get("data");
        List<SundayGoodsDto> goodss = new ArrayList<>();
        for (JSONObject object : goodJsonList) {
            SundayGoodsDto dto = new SundayGoodsDto(
                    object.getLong("goodsId"), object.getString("goodsCode"), object.getString("kcsl"), object.getString("goodsName"),
                    object.getString("goodsSurname"), object.getString("goodsColor"),  object.getString("goodsImg"));
            goodss.add(dto);
        }
        return new PageResult<SundayGoodsDto>(goodss, result.getLong("count"));
    }

    /**
     * 组装多个
     *
     * @param goodss
     * @return
     */
    public List<SundayGoodsDto> packageMultiDto(List<SundayGoods> goodss) {
        List<SundayGoodsDto> result = new ArrayList<>();
        if (ListUtil.isNotNull(goodss)) {
            for (SundayGoods goods : goodss) {
                SundayGoodsDto dto = new SundayGoodsDto();
                BeanUtils.copyProperties(goods, dto);
                result.add(dto);
            }
        }
        return result;
    }

    /**
     * 组装单个
     *
     * @param goods
     * @return
     */
    private SundayGoodsDto packageSingleDto(SundayGoods goods) {
        SundayGoodsDto dto = new SundayGoodsDto();
        BeanUtils.copyProperties(goods, dto);
        return dto;
    }


}
